import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { LocalStorage } from '@core/local.storage';
import { DatePipe } from '@angular/common';
import { Router, ActivatedRoute } from '@angular/router';
import { _HttpClient } from '@delon/theme';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-workareadetail',
    templateUrl: './workareadetail.component.html',
    styleUrls: ['./dispatchList.component.less']
})
export class WorkareaDetailComponent implements OnInit {
    operaData: any = {};
    rowThree = { 'display': 'inline-block', 'width': '150px', 'text-align': 'right' };
    stationSelect: any = [];
    devNameSelect: any = [];
    workTypeSelect: any = [];
    queryParams: any = {};
    sfVisible = false;
    wtkVisible = false;
    sanfangTemp: any = [];
    sanfangList: any = [];
    skywinList: any = [];
    problemList: any = [];
    problemListMore: any = [];
    peopleList: any;
    apiUrl = environment.project + '/ymTDispatchlist/update';
    readStatus = false;

    constructor(
        private msg: NzMessageService,
        private activatedRoute: ActivatedRoute,
        public ls: LocalStorage,
        private datePipe: DatePipe,
        private router: Router,
        private http: _HttpClient
    ) { }

    ngOnInit() {
        this.activatedRoute.queryParams.subscribe(param => {
            Object.assign(this.queryParams, param);
            if (this.queryParams.vcTeammenberid) {
                this.queryParams.vcTeammenberid = this.queryParams.vcTeammenberid.split(',');
            }
            this.readStatus = this.queryParams.vcStatus === '0' || this.queryParams.vcStatus === '3' ? false : true;
            // 室内外防护人员
            this.http.post(environment.devscore + '/orgPerson/queryList', { orgId: this.queryParams.vcOrgId, qualifictionId: '1' }).subscribe((res: any) => {
                if (!res.code) {
                    this.peopleList = res.data;
                }
            });
        });
        window.addEventListener('message', (data) => {
            if (data.data && data.data !== 'undefined' && JSON.stringify(data.data).indexOf('webpackOk') === -1) {
                this.sanfangTemp.push(data.data.text + " : " + data.data.tips + "<br>");
                this.sanfangList = this.unique(this.sanfangTemp);
            }
        });
        this.http.get(environment.project + '/dayWorkSgrwxkm/query?vcSta=' + this.queryParams.vcStaid + '&dtWorkDay=' + this.queryParams.dtWorkdate + '&orgId=' + this.queryParams.vcOrgId).subscribe((res: any) => {
            this.skywinList = res.meta.list;
        });
        let undone = '';
        undone = !this.queryParams.vcDevgroupId ? environment.project + '/safeProblem/queryUndone?devId=' + this.queryParams.vcDevid + '&stationId=' + this.queryParams.vcStaid : environment.project + '/safeProblem/queryUndone?stationId=' + this.queryParams.vcStaid;

        this.http.get(undone).subscribe((res: any) => {
            this.problemList = res;
            if (this.queryParams.vcProbids) {
                for (const item of this.problemList.data) {
                    if (this.queryParams.vcProbids.indexOf(item.id) !== -1) {
                        item.checked = true;
                    }
                }
            }
        });
    }
    // 去重
    unique(arr) {
        return Array.from(new Set(arr));
    }
    back() {
        this.router.navigate(['dispatchList/' + this.queryParams.typeName]);
    }
    sanfang() {
        this.sfVisible = true;
    }
    wtk() {
        this.wtkVisible = true;
    }
    sfOk() {
        this.sfVisible = false;
        this.queryParams.vcRiskinfo = this.sanfangList.toString();
        this.sanfangTemp = [];
    }
    sfCancal() {
        this.sfVisible = false;
    }
    wtkOk() {
        this.wtkVisible = false;
    }
    wtkCancal() {
        this.wtkVisible = false;
    }
    skywinChange(event) {
        this.skywinList.forEach(data => {
            if (data.nId === event) {
                this.queryParams.vcSkyplanrange = data.vcProject;
                this.queryParams.dtSkywinplantime = data.vcSkywinstime;
                this.queryParams.dtSkywinplanetime = data.vcSkywinetime;
            }
        });
    }
    saveDate() {
        const tempparams: any = {};
        Object.assign(tempparams, this.queryParams);
        tempparams.waName = "";
        tempparams.wsName = "";
        tempparams.staName = "";
        tempparams.dtSkywinplanetime = this.datePipe.transform(tempparams.dtSkywinplanetime, 'yyyy-MM-dd HH:mm:ss');
        tempparams.dtSkywinplantime = this.datePipe.transform(tempparams.dtSkywinplantime, 'yyyy-MM-dd HH:mm:ss');
        tempparams.vcTeammembername = tempparams.vcTeammembername ? tempparams.vcTeammembername.toString() : '';
        tempparams.vcOutguardid = tempparams.vcOutguardid ? tempparams.vcOutguardid.toString() : ' ';
        tempparams.vcToolsmaterial = tempparams.vcToolsmaterial ? tempparams.vcToolsmaterial.replace(/，/g, ',') : '';
        tempparams.vcWorkinfo = tempparams.vcWorkinfo ? tempparams.vcWorkinfo.replace(/，/g, ',') : '';
        tempparams.vcIndoornum = tempparams.vcIndoornum ? tempparams.vcIndoornum : '';
        tempparams.vcOutdoornum = tempparams.vcOutdoornum ? tempparams.vcOutdoornum : '';
        tempparams.vcProbids = [];
        tempparams.vcProbdesc = [];
        if (tempparams.sedate === 'null ~ null') {
            tempparams.sedate = "";
        }
        for (const item of this.problemList.data) {
            if (item.checked) {
                tempparams.vcProbids.push(item.id);
                tempparams.vcProbdesc.push(item.problemDesc);
            }
        }
        tempparams.vcProbids = tempparams.vcProbids ? tempparams.vcProbids.toString() : '';
        tempparams.vcProbdesc = tempparams.vcProbdesc ? tempparams.vcProbdesc.toString() : '';
        tempparams.vcTeammenberid = tempparams.vcTeammenberid ? tempparams.vcTeammenberid.toString() : '';

        this.http.post(this.apiUrl, tempparams).subscribe((res: any) => {
            if (!res.code) {
                this.router.navigate(['dispatchList/workarea']);
            } else {
                if (res.msg) {
                    this.msg.error(res.msg);
                } else {
                    this.msg.error('操作失败');
                }
            }
        });
    }
}
